package com.test.redis;

import java.util.HashMap;
import java.util.Map;

/**
 *  map负责查找，构建一个虚拟的双向列表，里边装的是一个个Node结点，作为数据载体
 */
public class LRUDemo2 {

    private int catchSize;
    Map<Integer,Node<Integer,Integer>> map;
    DoubleLinkedList<Integer,Integer> doubleLinkedList;

    public LRUDemo2(int catchSize) {
        this.catchSize = catchSize; // 缓存坑位
        map =  new HashMap<>(); //查找
        doubleLinkedList = new DoubleLinkedList<>();
    }

    public int get(int key){
        if(!map.containsKey(key)){
            return -1;
        }else{
            Node<Integer, Integer> node = map.get(key);
            doubleLinkedList.removeNode(node);
            doubleLinkedList.addHead(node);
            return node.value;
        }
    }

    public void put (int key,int value){
        if(map.containsKey(key)){ //update
            Node<Integer, Integer> node = map.get(key);
            node.value = value;
            map.put(key,node);

            doubleLinkedList.removeNode(node);
            doubleLinkedList.addHead(node);
        }else{
            if(map.size() == catchSize){ //坑位满了需要删除最后一个结点
                Node<Integer, Integer> last = doubleLinkedList.getLast();
                map.remove(last.key);
                doubleLinkedList.removeNode(last);
            }

            //新增
            Node<Integer,Integer> newNode = new Node<>(key,value);
            map.put(key,newNode);
            doubleLinkedList.addHead(newNode);


        }
    }

    //1.构建Node结点
    class Node<K,V>{
        K key;
        V value;
        Node<K,V> prev;
        Node<K,V> next;

        public Node() {
            this.prev = this.next = null;
        }

        public Node(K key, V value) {
            this.key = key;
            this.value = value;
            this.prev = this.next = null;
        }
    }

    //2.构造虚拟双向链表，放置Node结点，类似AQS
    class DoubleLinkedList<K,V>{
        Node<K,V> head;
        Node<K,V> tail;

        //2.1 构造方法
        public DoubleLinkedList() {
           head = new Node<>(); //初始化头结点null
           tail = new Node<>();//初始化尾结点null
           head.next = tail;//头结点的next指向tail
           tail.prev=head;//尾结点的prev指向head
        }
        //2.2 添加到头
        public void addHead(Node<K,V> node){
            node.next = head.next;
            node.prev = head;
            head.next.prev = node;
            head.next = node;
        }
        //2.3删除结点
        public void removeNode(Node<K,V> node){
            node.next.prev = node.prev;
            node.prev.next = node.next;
            node.prev = null;
            node.next = null;
        }

        //2.4 获得最后一个结点
        public Node<K,V> getLast(){
            return tail.prev;
        }
    }

    public static void main(String[] args) {
        LRUDemo2 demo2 = new LRUDemo2(3);
        demo2.put(1,1);
        demo2.put(2,1);
        demo2.put(3,1);
        System.out.println(demo2.map.keySet());
        demo2.put(4,1);
        System.out.println(demo2.map.keySet());
        demo2.put(3,1);
        System.out.println(demo2.map.keySet());
        demo2.put(3,1);
        System.out.println(demo2.map.keySet());
        demo2.put(5,1);
        System.out.println(demo2.map.keySet());

    }

}
